# -*- coding: utf-8 -*-
__author__ = 'Jinkey'

from keras.models import Sequential
from keras.layers import Dense, Activation,Dropout
from keras.datasets import mnist
from keras.models import load_model
import numpy as np
import time
import random
from theano import function, config, shared, tensor, sandbox
import numpy

time1 = time.time()
X_train ,y_train,X_test,y_test = [],[],[],[]

for x in range(1,500000):
    X_train.append([random.randint(0,100),random.randint(200,500)])
    y_train.append(0)
    X_test.append([random.randint(0, 100), random.randint(200, 500)])
    y_test.append(0)

for x in range(1,100000):
    X_train.append([random.randint(200,500),random.randint(0,100)])
    y_train.append(1)
    X_test.append([random.randint(200, 500), random.randint(0, 100)])
    y_test.append(1)

print(X_train)
print(y_train)

model = Sequential()
model.add(Dense(output_dim=1, input_dim=2, activation='sigmoid'))


model.compile(loss='mse', optimizer="sgd", metrics=['accuracy'])
model.fit(np.array(X_train), np.array(y_train),validation_split=0.2,
    batch_size=128, nb_epoch=100, verbose=0)

print "="*50
score = model.evaluate(np.array(X_test), np.array(y_test))
print('Test score:', score[0])
print('Test accuracy:', score[1])
model.save('jinkey.h5')

model = load_model('jinkey.h5')
print(model.predict_classes(np.array([[1000, 250], [64, 600]])))

time2 = time.time()

print("用时 %f" %(time2-time1))

f = function([], tensor.exp(x))
if numpy.any([isinstance(x.op, tensor.Elemwise) and
                      ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')